package com.decent.ejfadmin.order.dao;

import com.decent.ejfadmin.common.bean.ExkSupplyCard;
import com.decent.ejfadmin.common.bean.SupplyCard;
import com.decent.ejfadmin.order.dao.provider.OrderDaoProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * describe:订单管理
 *
 * @author 王元鑫
 * @date 2019/09/07
 */
@Mapper
@Repository("OrderDao")
public interface OrderDao {

    /**
     * 更新销卡订单
     *
     * @param orderId     订单号
     * @param status      状态
     * @param settlePrice 结算金额
     * @param dbStatus    数据库原状态
     * @param failReason  停售原因，可空
     * @param userPrice   符合实际的上传价格
     * @return 更新行数
     */
    @UpdateProvider(type = OrderDaoProvider.class, method = "modifySupplyOrder")
    int modifySupplyOrder(@Param("orderId") String orderId, @Param("status") Integer status,
                          @Param("settlePrice") Double settlePrice, @Param("dbStatus") Integer dbStatus,
                          @Param("failReason") String failReason, @Param("userPrice") double userPrice,
                          @Param("judgeFace") BigDecimal judgeFace);

    /**
     * 根据对内订单号查询销卡订单
     *
     * @param orderId 对内订单号
     * @return 销卡订单
     */
    @Select("SELECT * FROM yuhuang_supply_card WHERE orderId = #{orderId}")
    SupplyCard querySupplyOrderByOrderId(@Param("orderId") String orderId);

    /**
     * 补结算销卡订单
     *
     * @param orderId     订单号
     * @param actualPrice 销卡金额
     * @param userPrice   符合实际的上传价格
     * @return 修改行数
     */
    @Update("update yuhuang_supply_card set status = 1 ,settlePrice = #{price} ,endTime = now() , added = 1 , failReason = '23000' , userPrice = #{userPrice} where orderId = #{orderId} and status = 2")
    int makeUpSupplyOrder(@Param("orderId") String orderId, @Param("price") BigDecimal actualPrice, @Param("userPrice") double userPrice);

    /**
     * 销卡订单查询面值不符率
     *
     * @param card 查询信息
     * @return list(wrongFace错卡数, preJudge未预判数, total总数)
     */
    @Select("SELECT\n" +
            "       sum(if(status = #{status} , 1, 0)) preJudge,\n" +
            "       count(1)                                          total\n" +
            "FROM yuhuang_supply_card\n" +
            "WHERE startTime >= #{card.startTime} - 1000 * 60 * 5 AND startTime <= #{card.startTime} + 1000 * 60 * 5 AND\n" +
            "      batchId = #{card.batchId} AND productType = #{card.productType} ")
    HashMap<String, Object> queryWrongCardRate(@Param("card") SupplyCard card, @Param("status") Integer status);

    /**
     * 销卡订单批次面值不符暂停销卡
     *
     * @param card       查询条件
     * @param status     数据库原状态
     * @param failReason 错误编码
     */
    @Update("UPDATE yuhuang_supply_card " +
            " SET status = 5, endTime = now(), failReason = #{failReason} " +
            " WHERE startTime >= DATE_SUB(#{card.startTime},INTERVAL 5 MINUTE) " +
            " AND startTime <= DATE_ADD(#{card.startTime},INTERVAL 5 MINUTE) " +
            " AND  batchId = #{card.batchId} AND productType = #{card.productType} " +
            " AND status = #{status}")
    int batchWrongFaceSupplyCard(@Param("card") SupplyCard card, @Param("status") Integer status, @Param("failReason") String failReason);


    /**
     * 收卡明细查询同步功能,查询需要同步的订单
     *
     * @param ids ID
     * @return list
     */
    @Select({"SELECT * FROM yuhuang_supply_card WHERE id IN (#{ids}) AND status IN (0,3,8)"})
    List<SupplyCard> querySynchronousCard(@Param("ids") String ids);


    /**
     * 查询会员未异步的需要异步的订单
     *
     * @param userId userId
     * @return list
     */
    @Select({"SELECT * FROM yuhuang_supply_card WHERE userId = #{userId} AND status IN (1,2) AND asynStatus=0 AND asynTime >= DATE_SUB(NOW(), INTERVAL 2 DAY)"})
    List<SupplyCard> queryAsynchronousCard(@Param("userId") Long userId);

    /**
     * 更新易销卡销卡订单
     *
     * @param orderId       订单号
     * @param status        状态
     * @param settlePrice   结算金额
     * @param time          处理时间
     * @param dbStatus      数据库原状态
     * @param failReason    停售原因，可空
     * @param agentNo       易销卡对应的商户编号
     * @param mobileNo      卡对应的充值手机号
     * @param cost          号的成本  对应易缴费or易服务的userPrice即交易金额
     * @param userPrice     符合实际的上传价格
     * @param exkSupplyCard 参数封装实体
     * @return 更新行数
     */
    @SuppressWarnings("JavadocReference")
    @UpdateProvider(type = OrderDaoProvider.class, method = "modifyExkSupplyOrder")
    int modifyExkSupplyOrder(@Param("exkCard") ExkSupplyCard exkSupplyCard);
}